<template>
  <div class="login">
    <div class="box">
      <h1 align="center">梦学谷会员管理系统</h1>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model.trim="ruleForm.username" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" show-password v-model.trim="ruleForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width:100%;" type="primary" @click="doLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  mounted() {},
  methods: {
      doLogin(){
          this.$refs['ruleForm'].validate(valid=>{
              if(valid){
                  this.$store.dispatch('doLogin', this.ruleForm).then(res=>{
                      console.log(res);
                      if(res.data.flag){
                          this.$message.success(res.data.message)
                          this.$router.push('/layout')
                      }else{
                          this.$message.error(res.data.message)
                      }
                  }).catch(error=>{
                      console.log(error);
                  })
              }
          })
      }
  },
};
</script>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  background: url("../../assets/login.jpg") no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  .box {
    width: 350px;
    padding: 30px;
    border-radius: 20px;
    background-color: rgba(255, 255, 255, 0.8);
    h1 {
      font-size: 20;
      font-weight: bold;
      margin-bottom: 20px;
    }
  }
}
</style>
